﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using JSIF_WCFServer.Misc.Logging;

namespace JSIF_WCFServer.Misc.Extensions
{
    /// <summary>
    /// Defines common extension methods
    /// </summary>
    public static class ExtensionMethods
    {
        private static GenericLogger log = GenericLogger.GetLogger("ExtensionMethods");

        /// <summary>
        /// Returns true if the directory on the left is the parent of the directory on the right
        /// </summary>
        /// <param name="potentialParent">the directory which is potentially the parent of the other</param>
        /// <param name="potentialChild">the directory which is potentially the child of the other</param>
        /// <returns>true if the directory on the left is the parent of the directory on the right</returns>
        public static bool isParentOf(this DirectoryInfo potentialParent, DirectoryInfo potentialChild)
        {
            DirectoryInfo temp = potentialChild;
            while (temp != null)
            {
                if (temp.Equals(potentialParent))
                {
                    return true;
                }
                temp = temp.Parent;
            }
            return false;
        }

        /// <summary>
        /// Returns true if the specified directory is a subversion repository
        /// </summary>
        /// <param name="repository">A subversion repository client (not actually used, but this is where the method should logically be)</param>
        /// <param name="potentialRepository">The directory which is potentially a subversion repository</param>
        /// <returns>true if the specified directory is a subversion repository</returns>
        public static bool isRepository(this SharpSvn.SvnRepositoryClient repository, DirectoryInfo potentialRepository)
        {
            try
            {
                SharpSvn.SvnClient c = new SharpSvn.SvnClient();
                System.Collections.ObjectModel.Collection<SharpSvn.SvnLogEventArgs> dummy;
                bool result = c.GetLog(new Uri("file://" + potentialRepository.FullName.Replace("\\", "/")), out dummy);
                return result;
            }
            catch (Exception e)
            {
                log.Debug(e, "exception occured while retriving svn log");
                return false;
            }
        }
    }
}
